package com.reserv.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.reserv.dao.ZipCodeDAO;
import com.reserv.vo.ZipCodeVO;

public class AddrFindServlet extends HttpServlet {
	private ZipCodeDAO dao;
	@Override
	public void init(ServletConfig config) throws ServletException {
		dao = ZipCodeDAO.getInstance();
	}	
	
	private void doProcess(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		ArrayList<ZipCodeVO> list = (ArrayList<ZipCodeVO>) dao.getListVOs(request.getParameter("dong"));
		
		System.out.println(list);
		if(list != null) {
			for(ZipCodeVO zc : list) {
				System.out.println(zc);
				zc.setBunji(URLEncoder.encode(zc.getBunji(), "utf-8"));
				zc.setDong(URLEncoder.encode(zc.getDong(), "utf-8"));
				zc.setGugun(URLEncoder.encode(zc.getGugun(), "utf-8"));
				zc.setRi(URLEncoder.encode(zc.getRi(), "utf-8"));
				zc.setSido(URLEncoder.encode(zc.getSido(), "utf-8"));
				zc.setZipCode(URLEncoder.encode(zc.getZipCode(), "utf-8"));
			}
			Gson gson = new Gson();
			PrintWriter out = response.getWriter();
			String json = gson.toJson(list);
			out.println(json);
		}
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doProcess(request, response);
	}
}
